// pages/ends/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {
      avatarUrl: "",//用户头像  
      nickName: "",//用户昵称  
    },
    myselfScore: "",
    oppentScore: "",
    result: "../images/success.png",
    reward: "",
    myDisplay: "",
    opDisplay: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getUserInfo({
      success: function (res) {
        console.log(res);
        var avatarUrl = 'userInfo.avatarUrl';
        var nickName = 'userInfo.nickName';
        that.setData({
          [avatarUrl]: res.userInfo.avatarUrl,
          [nickName]: res.userInfo.nickName,
        })
      }
    })
    this.setData({
      myselfScore: getApp().globalData.myscore,
      oppentScore: getApp().globalData.opscore,
      hisHead: getApp().globalData.OpponentAvatarUrl,
      hisName: getApp().globalData.Opponentnickname,

    })
    var myselfScore = that.data.myselfScore;
    var oppentScore = that.data.oppentScore;
    if (myselfScore > oppentScore) {
      this.setData({
        result: "../images/success.png",
        reward: "+1",
        opDisplay: "none",
      })
      this.win()
    } if (myselfScore == oppentScore) {
      this.setData({
        result: "../images/draw.png",
        reward: "0",
        opDisplay: "none",
        myDisplay: "none",
      })
      this.same()
    } if (myselfScore < oppentScore) {
      this.setData({
        result: "../images/fail.png",
        reward: "-1",
        myDisplay: "none",
      })
      this.lose()
    }
  },
  win: function () {
    wx.request({
      url: getApp().globalData.url +'/computeScore.do',
      data:{
        openid : getApp().globalData.openid,
        flag : 1
      },
      header: {'content-type': 'json' },
      success: function (res) {
        console.log('分数上传成功');
      }
    })
  },
  lose: function () {
    wx.request({
      url: getApp().globalData.url +'/computeScore.do',
      data: {
        openid: getApp().globalData.openid,
        flag: 0
      },
      header: { 'content-type': 'json' },
      success: function (res) {
        console.log('分数上传成功');
      }
    })
  },
  same: function () {
    wx.request({
      url: getApp().globalData.url +'/computeScore.do',
      data: {
        openid: getApp().globalData.openid,
        flag: 2
      },
      header: { 'content-type': 'json' },
      success: function (res) {
        console.log('分数上传成功');
      }
    })
  },

  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '社科知识答题小程序',
      path: '/pages/Authorization/index',
      imageUrl: '../images/meeting.jpg'
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  goon: function (event) {
    wx.redirectTo({
      url: '../answer/index'
    })
  },
  gotoHomepage: function (event) {
    wx.redirectTo({
      url: '../index/index',
    })
  }
})